package com.cloudcc.mobile.view.market;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckByPhoneEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketRefreshInfo;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.presenter.MarketActivityPresenter;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;
import com.litesuits.android.log.Log;

public class MarketCheckPhoneFragment extends BaseFragment implements
		OnTitleBarClickListener {
	private String mEn = RunTimeManager.getInstance().getlanguage();
	private boolean mBo;

	@Bind(R.id.market_tv_phone_name)
	TextView market_tv_phone_name;
	@Bind(R.id.market_tv_phone_number)
	TextView market_tv_phone_number;
	@Bind(R.id.market_btn_check)
	Button market_btn_check;

	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;

	private MarketActivityPresenter mMarketPresenter = new MarketActivityPresenter();

	private String mMarketActivityId;
	private String mPhoneNumber;
	private String mName;

	@Override
	public void init() {
		initHeader();
		refreshInfoData();
		headerbar.completeProgress();
	}

	/**
	 * 初始化标题
	 */
	private void initHeader() {
		if ("en".equals(mEn)) {
			mBo = true;	
		}else{
			mBo = false;
		}
	
		mPhoneNumber = getArguments().getString("market.phone", "");
		mName = getArguments().getString("market.name", "");
		mMarketActivityId = getArguments()
				.getString(ExtraConstant.EXTRA_ID, "");
		headerbar.setOnTitleBarClickListener(this);
	}

	/**
	 * 刷新info 数据
	 */
	private void refreshInfoData() {
		ViewUtils.setText(market_tv_phone_name, mName);
		ViewUtils.setText(market_tv_phone_number, mPhoneNumber);
		market_btn_check.setEnabled(true);

	}

	@OnClick(R.id.market_btn_check)
	public void onClickChect() {
		Log.d("zhengzaiqiandao", "zhengzaiqiandao");
		if (mBo) {
			showWainting("Please wait, sign in ...");
		}else{
			showWainting("请稍等,正在签到...");
		}
		
		mMarketPresenter.checkByPhone(mPhoneNumber, mMarketActivityId);
	}

	@OnClick(R.id.market_btn_back)
	public void onClickFinish() {// 返回
		onClickLeft(null);
	}

	public void onEventMainThread(MarketCheckByPhoneEvent event) {
		Log.d("getMessage", event.getMessage().toString() + "测试");

		dismissWainting();
		headerbar.completeProgress();
		if (event.isError()) {
			Toast.makeText(mContext, event.getMessage(), 0).show();
			return;
		}
		
		if (mBo) {
			showToast("Sign in success...");
		}else{
			showToast("签到成功...");
		}
		onClickLeft(null);
		// 刷新市场活动详情
		Log.d("getMessage", event.getMessage());
		MarketRefreshInfo marketRefreshInfo = new MarketRefreshInfo();
		Log.d("getMessage1", "签到成功返回" + marketRefreshInfo.getMessage()
				+ "  MarketRefreshInfo");
		marketRefreshInfo.setOk(true);
		EventEngine.post(marketRefreshInfo);
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {

	}

	@Override
	public int getLayoutId() {
		return R.layout.market_checkphone_frag;
	}

	@Override
	protected boolean hasEventLife() {
		return true;
	}

}
